package com.rd.student.ruanjian.class4.lichunquan.Text02;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

public class Test06 {
    //实现接口进行升降序
    public static void main(String[] args) {
        ArrayList arrayList = new ArrayList();
        Student01 s0 = new Student01("liusan ",20,90);
        Student01 s1 = new Student01("lisi  ",22,90);
        Student01 s2 = new Student01("wangwu ",20,99);
        Student01 s3 = new Student01("sunliu ",22,100);
        arrayList.add(s0);
        arrayList.add(s1);
        arrayList.add(s2);
        arrayList.add(s3);
        Collections.sort(arrayList);
        //Comparable升序遍历
        for (Object o : arrayList) {
            Student01 s = (Student01) o;
            System.out.println(s);
        }

        //Comparator降序排列
        Collections.sort(arrayList, new Comparator<Student01>() {
            @Override
            public int compare(Student01 o1, Student01 o2) {
                if (o1.getCord() < (o2).getCord()) {
                    return 1;
                } else if ( o1.getCord() > o2.getCord()) {
                    return -1;
                } else if (o1.getAge() >  o2.getAge()) {
                    return 1;
                } else if (o1.getAge() < o2.getAge()) {
                    return -1;
                }else{
                    return 0;
                }
            }
        });
        for (Object o : arrayList) {
            Student01 s = (Student01) o;
            System.out.println(s);
        }
    }
}
class Student01 implements Comparable{
    private String name;
    private int age;
    private int cord;
    public Student01(String name, int age, int cord) {
        this.name = name;
        this.age = age;
        this.cord = cord;
    }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public int getAge() { return age; }
    public void setAge(int age) {this.age = age;}
    public int getCord() { return cord; }
    public void setCord(int cord) { this.cord = cord;}
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", cord=" + cord +
                '}';
    }
    //降序
    @Override
    public int compareTo(Object o) {
        if (this.cord > ((Student01) o).getCord()) {
            return 1;
        } else if (this.cord < ((Student01) o).getCord()) {
            return -1;
        } else if (this.age > ((Student01) o).getAge()) {
            return 1;
        } else if (this.age < ((Student01) o).getAge()) {
            return -1;
        }else{
            return 0;
        }
    }
}
